------------------------------------
--	Simple Admin System
--	File: Commands.lua
--	By Spacetech
--	License: http://creativecommons.org/licenses/by-nc-sa/3.0/
------------------------------------
--	SAS.AddChatCommand(CommandName, Function, UserGroup, ArgsNumber)
------------------------------------

function SAS.Kick(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(SAS.AdminCheck(ply, args[1])) then
		SAS.Nofity(ply, "This player has immunity")
	else
		game.ConsoleCommand("kickid "..Target:UserID().."\n")
		SAS.PrintAll(ply:Nick().." has kicked "..Target:Nick())
		SAS.LogEvent("#"..ply:Nick().." has kicked "..Target:Nick())
	end
end
SAS.AddChatCommand("Kick", SAS.Kick, "Moderator", 1)

function SAS.Ban(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(SAS.AdminCheck(ply, args[1])) then
		SAS.Nofity(ply, "This player has immunity")
	elseif(args[2] && tonumber(args[2]) == nil) then
		SAS.Nofity(ply, args[2].." is not a valid ban time number")
	else
		if(!args[2]) then
			game.ConsoleCommand("banid 0 "..Target:UserID().." kick\n")
			game.ConsoleCommand("writeid\n")
			SAS.PrintAll(ply:Nick().." has banned "..Target:Nick())
			SAS.LogEvent("#"..ply:Nick().." has banned "..Target:Nick())
		else
			game.ConsoleCommand("banid "..args[2].." "..Target:UserID().." kick\n")
			game.ConsoleCommand("writeid\n")
			SAS.PrintAll(ply:Nick().." has banned "..Target:Nick().." for "..args[2].." minutes")
			SAS.LogEvent("#"..ply:Nick().." has banned "..Target:Nick().." for "..args[2].." minutes")
		end
	end
end
SAS.AddChatCommand("Ban", SAS.Ban, "Admin", 1)

function SAS.Freeze(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(SAS.AdminCheck(ply, args[1])) then
		SAS.Nofity(ply, "This player has immunity")
	elseif(SAS.Frozen[Target:SteamID()] == true) then
		Target:UnLock()
		SAS.Frozen[Target:SteamID()] = false
		SAS.PrintAll(ply:Nick().." has unfreezed "..Target:Nick())
		SAS.LogEvent("#"..ply:Nick().." has unfreezed "..Target:Nick())
	else
		Target:Lock()
		SAS.Frozen[Target:SteamID()] = true
		SAS.PrintAll(ply:Nick().." has freezed "..Target:Nick())
		SAS.LogEvent("#"..ply:Nick().." has freezed "..Target:Nick())
	end
end
SAS.AddChatCommand("Freeze", SAS.Freeze, "Moderator", 1)

function SAS.God(ply, args)
	local Target = "none"
	if(args[1] && args[1] != "") then
		Target = SAS.CheckPlayer(args[1])
	end
	if(Target == "none") then
		if(SAS.CGod[ply:SteamID()] == true) then
			ply:GodDisable()
			SAS.CGod[ply:SteamID()] = false
			SAS.PrintAll(ply:Nick().." has ungodded him/herself")
			SAS.LogEvent("#"..ply:Nick().." has ungodded him/herself")
		else
			ply:GodEnable() 
			SAS.CGod[ply:SteamID()] = true
			SAS.PrintAll(ply:Nick().." has godded him/herself")
			SAS.LogEvent("#"..ply:Nick().." has godded him/herself")
		end
	elseif(Target == false) then
		SAS.Nofity(ply, "No player was found")
	else
		if(SAS.CGod[Target:SteamID()] == true) then
			Target:GodDisable()
			SAS.CGod[Target:SteamID()] = false
			SAS.PrintAll(ply:Nick().." has ungodded "..Target:Nick())
			SAS.LogEvent("#"..ply:Nick().." has ungodded "..Target:Nick())
		else
			Target:GodEnable() 
			SAS.CGod[Target:SteamID()] = true
			SAS.PrintAll(ply:Nick().." has godded "..Target:Nick())
			SAS.LogEvent("#"..ply:Nick().." has godded "..Target:Nick())
		end
	end
end
SAS.AddChatCommand("God", SAS.God, "Moderator", 0)

function SAS.Goto(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	else
		ply:SetPos(Target:GetPos())
		SAS.PrintAll(ply:Nick().." has teleported to "..Target:Nick())
		SAS.LogEvent("#"..ply:Nick().." has teleported to "..Target:Nick())
	end
end
SAS.AddChatCommand("Goto", SAS.Goto, "Moderator", 1)

function SAS.Bring(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	else
		Target:SetPos(ply:GetPos())
		SAS.PrintAll(ply:Nick().." has brought "..Target:Nick())
		SAS.LogEvent("#"..ply:Nick().." has brought "..Target:Nick())
	end
end
SAS.AddChatCommand("Bring", SAS.Bring, "Moderator", 1)

function SAS.Slay(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(SAS.AdminCheck(ply, args[1]) == true) then
		SAS.Nofity(ply, "This player has immunity")
	else
		Target:Kill()
		SAS.PrintAll(ply:Nick().." has slayed "..Target:Nick())
		SAS.LogEvent("#"..ply:Nick().." has slayed "..Target:Nick())
	end
end
SAS.AddChatCommand("Slay", SAS.Slay, "Moderator", 1)

function SAS.Noclip(ply, args)
	local Target = "none"
	if(args[1] && args[1] != "") then
		Target = SAS.CheckPlayer(args[1])
	end
	if(Target == "none") then
		if(ply:GetMoveType() == MOVETYPE_WALK) then
			ply:SetMoveType(MOVETYPE_NOCLIP)
			SAS.LogEvent("#"..ply:Nick().." has noclipped")
		elseif(ply:GetMoveType() == MOVETYPE_NOCLIP) then
			ply:SetMoveType(MOVETYPE_WALK)
			SAS.LogEvent("#"..ply:Nick().." has unnoclipped")
		end
	elseif(Target == false) then
		SAS.Nofity(ply, "No player was found")
	else
		if(Target:GetMoveType() == MOVETYPE_WALK) then
			Target:SetMoveType(MOVETYPE_NOCLIP)
			SAS.PrintAll(ply:Nick().." has noclipped "..Target:Nick())
			SAS.LogEvent("#"..ply:Nick().." has noclipped "..Target:Nick())
		elseif(Target:GetMoveType() == MOVETYPE_NOCLIP) then
			Target:SetMoveType(MOVETYPE_WALK)
			SAS.PrintAll(ply:Nick().." has unnoclipped "..Target:Nick())
			SAS.LogEvent("#"..ply:Nick().." has unnoclipped "..Target:Nick())
		end
	end
end
SAS.AddChatCommand("Noclip", SAS.Noclip, "Moderator", 0)

function SAS.Rcon(ply, args)
	SAS.LogEvent("#"..ply:Nick().." has rconed the server with the command: "..args[1])
	game.ConsoleCommand(args[1].."\n")
end
SAS.AddChatCommand("Rcon", SAS.Rcon, "Super Admin", 1)

function SAS.Restart(ply, args)
	SAS.PrintAll(ply:Nick().." has restarted the server")
	SAS.LogEvent("#"..ply:Nick().." has restarted the server")
	game.ConsoleCommand("quit\n")
end
SAS.AddChatCommand("Restart", SAS.Restart, "Super Admin", 0)

function SAS.Map(ply, args)
	--Thanks@ Neico and/or Megiddo for this map function
	local Maps = {}
	local Map = file.Find("../maps/*.bsp")
	for _, MapV in pairs(file.Find("../maps/*.bsp")) do
		if(string.find(MapV, "_")) then
			Maps[#Maps + 1] = MapV:sub(1, -5)
		end
	end
	table.sort(Maps)
  
	local FoundIt = false
	for k, v in pairs(Maps) do
		if(string.find(args[1], v)) then
			FoundIt = true
			SAS.PrintAll(ply:Nick().." has changed the map to "..args[1])
			SAS.LogEvent("#"..ply:Nick().."has changed the map to "..args[1])
			game.ConsoleCommand("changelevel "..v.."\n")
		end
	end
	if(FoundIt == false) then
		SAS.Nofity(ply, "No map was found")
	end
end
SAS.AddChatCommand("Map", SAS.Map, "Admin", 1)

function SAS.Cleanup(ply, args)
	local Target = "none"
	if(args[1] && args[1] != "") then
		Target = SAS.CheckPlayer(args[1])
	end
	if(Target == "none") then
		for k,v in pairs(player.GetAll()) do
			cleanup.CC_Cleanup(v, "gmod_cleanup", {})
		end
		SAS.PrintAll(ply:Nick().." has cleaned the server")
		SAS.LogEvent("#"..ply:Nick().." has cleaned the server")
	elseif(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(SAS.AdminCheck(ply, args[1])) then
		SAS.Nofity(ply, "This player has immunity")
	else
		cleanup.CC_Cleanup(Target, "gmod_cleanup", {})
		SAS.PrintAll(ply:Nick().." has cleanup "..Target:Nick().."'s prop's")
		SAS.LogEvent("#"..ply:Nick().." has cleanup "..Target:Nick().."'s prop's")	
	end
end
SAS.AddChatCommand("Cleanup", SAS.Cleanup, "Moderator", 0)

function SAS.SuperAdmin(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(ply:IsUserGroup("superadmin") == true) then
		SAS.Nofity(ply, "This player is already a super admin")
	else
		Target:SetUserGroup("superadmin")
		--SAS.Nofity(ply, "Promotions will take effect on server reload/map change")
		SAS.PrintAll(ply:Nick().." has made "..Target:Nick().." an super admin")
		SAS.LogEvent("#"..ply:Nick().." has made "..Target:Nick().." an super admin")
	end
end
SAS.AddChatCommand("SuperAdmin", SAS.SuperAdmin, "Super Admin", 1)

function SAS.Admin(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(ply:IsUserGroup("admin") == true) then
		SAS.Nofity(ply, "This player is already a admin")
	else
		Target:SetUserGroup("admin")
		--SAS.Nofity(ply, "Promotions will take effect on server reload/map change")
		SAS.PrintAll(ply:Nick().." has made "..Target:Nick().." an admin")
		SAS.LogEvent("#"..ply:Nick().." has made "..Target:Nick().." an admin")
	end
end
SAS.AddChatCommand("Admin", SAS.Admin, "Super Admin", 1)

function SAS.Moderator(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(ply:IsUserGroup("moderator") == true) then
		SAS.Nofity(ply, "This player is already an admin")
	else
		Target:SetUserGroup("moderator")
		--SAS.Nofity(ply, "Promotions will take effect on server reload/map change")
		SAS.PrintAll(ply:Nick().." has made "..Target:Nick().." a moderator")
		SAS.LogEvent("#"..ply:Nick().." has made "..Target:Nick().." a moderator")
	end
end
SAS.AddChatCommand("Moderator", SAS.Moderator, "Super Admin", 1)

function SAS.Ungroup(ply, args)
	local Target = SAS.CheckPlayer(args[1])
	if(Target == false) then
		SAS.Nofity(ply, "No player was found")
	elseif(Target == ply) then
		SAS.Nofity(ply, "You can't ungroup yourself")
	else
		Target:SetUserGroup("")
		--SAS.Nofity(ply, "Un-Promotions will take effect on server reload/map change")
		SAS.PrintAll(ply:Nick().." has removed "..Target:Nick().." privileges")
		SAS.LogEvent("#"..ply:Nick().." has removed "..Target:Nick().." privileges")
	end
end
SAS.AddChatCommand("Ungroup", SAS.Ungroup, "Super Admin", 1)

function SAS.ACommands(ply, args)
	SAS.Print(ply, "Available Chat Commands (Prefix: "..SAS.ChatCommandPrefix..")")
	local Commands = ""
	for k, v in pairs(SAS.Commands) do
		if(Commands == "") then
			Commands = v[1]
		else
			Commands = Commands..", "..v[1]
		end
	end
	if(string.len(Commands) > 111) then
		local Commands1 = string.sub(Commands, 1, 111)
		local Commands2 = string.sub(Commands, 112)
		SAS.Print(ply, Commands1)
		SAS.Print(ply, Commands2)
	else
		SAS.Print(ply, Commands)
	end
end
SAS.AddChatCommand("Commands", SAS.ACommands, "", 0)

-- Simplest Chat Command Here
function SAS.SASInfo(ply, args)
	SAS.Nofity(ply, "This server is running Simple Admin System Version "..SAS_Version.." - By Spacetech")
end
SAS.AddChatCommand("SAS", SAS.SASInfo, "", 0)

-- Old Debug function. This script won't crash in a million years :D
-- function SAS.MakeError(ply, args)
	-- local GGError = table.Count(MADEUPTABLE)
-- end
-- SAS.AddChatCommand("MakeError", SAS.MakeError, "Super Admin", 0)
